package com.agu.mgt.spmgt.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.agu.utils.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.agu.mgt.spmgt.dao.SpUserDao;
import com.agu.entity.spmgt.SpUserEntity;
import com.agu.mgt.spmgt.service.SpUserService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service("spUserService")
@Transactional(readOnly = true)
public class SpUserService{
	@Autowired
	private SpUserDao spUserDao;
	
	
	public SpUserEntity queryObject(Long userId){
		return spUserDao.queryObject(userId);
	}
	
	
	public List<SpUserEntity> queryList(Map<String, Object> map){
		return spUserDao.queryList(map);
	}

    
	public Page<SpUserEntity> queryPageList(Query query){
	    PageHelper.startPage(query.getPage(), query.getLimit());
	    return spUserDao.queryPageList(query);
	}
	
	
	public int queryTotal(Map<String, Object> map){
		return spUserDao.queryTotal(map);
	}
	
	
	@Transactional
	public void save(SpUserEntity spUser){
		spUserDao.save(spUser);
		spUserDao.saveSpUser(spUser);
		
		spUserDao.deleteUserRose(spUser.getUserId());
		Map<String, Object> map = new HashMap<>();
		map.put("userId", spUser.getUserId());
		
		if(spUser.getUserType().intValue()==1)
		{
			map.put("roleId", 3);
		}else {
			map.put("roleId", 4);
		}
		
		spUserDao.addUserRose(map);
	}
	
	
	@Transactional
	public void update(SpUserEntity spUser){
		spUserDao.update(spUser);
		
		
		spUserDao.deleteUserRose(spUser.getUserId());
		
		Map<String, Object> map = new HashMap<>();
		map.put("userId", spUser.getUserId());
		
		if(spUser.getUserType().intValue()==1)
		{
			map.put("roleId", 3);
		}else {
			map.put("roleId", 4);
		}
		
		spUserDao.addUserRose(map);
	}
	
	
	@Transactional
	public void delete(Long userId){
		spUserDao.delete(userId);
	}
	
	
	@Transactional
	public void deleteBatch(Long[] userIds){
		spUserDao.deleteBatch(userIds);
	}
}
